package com.winit.openapi.apiadapter.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.pms.spi.v2.instance.pscInstance.ProductInstanceService;

/**
 * 最后一公里保险类型Controller适配，老的查询返回保险类型，新接口无保险，返回固定的默认值
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * minghao.deng 	1.0  		2016年12月12日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
public class QueryDeliveryInsuranceAdapteController extends AbstractAdapteController {

    @Resource
    private ProductInstanceService productInstanceService;

    @RequestMapping(value = "/queryDeliveryInsuranceType")
    public String queryDeliveryInsuranceType(@ModelAttribute RequestMsg requestMsg,
                                             @ModelAttribute ResponseMsg responseMsg) {

        return doAdapte(requestMsg, responseMsg);
    }

    @Override
    protected void validateInputJson(RequestMsg requestMsg) {
        // 获取参数
        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        if (null == json) {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 派送方式ID必填
        ValidateUtil.validNotNull(json, "deliveryWay");
        ValidateUtil.validMaxLength(json, "deliveryWay", 20, true);

    }

//    private String getWinitProductCode(String deliveryWayID) {
//        ProductInstance ps = null;
//        try {
//            ProductInstanceByDeliveryModeCommand command = new ProductInstanceByDeliveryModeCommand();
//            command.setDeliveryMode(deliveryWayID);
//            ps = productInstanceService.getProductByDeliveryMode(command);
//        } catch (Exception e) {
//            return null;
//        }
//        if (ps == null) {
//            return null;
//        }
//        return ps.getCode();
//    }

    @Override
    public boolean checkCustomerTansferConfig(RequestMsg requestMsg, ResponseMsg responseMsg) {
        // 获取参数
//        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
//
//        String deliveryWayId = json.getString("deliveryWay");
        // 如果在PMS2里面可以查询到派送方式对应的产品，则为true
        return true;
    }

    @Override
    public void doAdapteInOpenAPI(RequestMsg requestMsg, ResponseMsg responseMsg) {
        // 新系统目前不支持保险，固定No Insurance
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("insuranceID", 1000000);
        map.put("insuranceType", "No Insurance");
        list.add(map);
        responseMsg.setData(list);

    }

}
